package com.shj.em.base.controller;

import com.shj.em.aa.entity.User;
import com.shj.em.aa.service.UserService;
import com.shj.em.base.entity.BaseEntity;
import com.shj.em.biz.company.entity.Company;
import com.shj.em.biz.company.service.CompanyService;
import com.shj.em.biz.company.service.ContactService;
import com.shj.em.biz.sell.entity.Order;
import com.shj.em.biz.sell.service.OrderService;
import com.shj.em.biz.finance.service.ReceiptService;
import com.shj.em.biz.sys.service.CdService;
import com.shj.em.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@Slf4j
public class BaseController<T extends BaseEntity> {

    @Autowired
    protected CompanyService companyService;

    @Autowired
    protected UserService userService;

    @Autowired
    protected CdService cdService;

    @Autowired
    protected OrderService orderService;

    @Autowired
    protected ReceiptService receiptService;

    @Autowired
    protected ContactService contactService;

    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true 为允许空值
    }

    protected void addRoleInfo(Model model){
        boolean isFinance = ShiroUtils.isFinancial();
        boolean isSell = ShiroUtils.isSell();
        boolean isPurchase = ShiroUtils.isPurchase();
        boolean isManager = ShiroUtils.isManager();
        boolean isProd = ShiroUtils.isProd();
        boolean isSupply = ShiroUtils.isSupply();

        model.addAttribute("canAddPayment", isFinance);
        model.addAttribute("canAddWaiFa", isPurchase);
        model.addAttribute("canEditOrder", isSell);
        model.addAttribute("canDelOrder", isManager);
        model.addAttribute("canAddEffort", isProd);
        model.addAttribute("canEditOutDate", isSupply);

        boolean canCloseOrder = ShiroUtils.hasPermission("order:close");
        model.addAttribute("canCloseOrder", canCloseOrder);
    }

    protected void addViewOrderInfo(Long orderId, Model model){
        Order order = orderService.selectById(orderId);
        Company company = companyService.selectById(order.getCustId());
        order.setCustName(company.getName());

        User user = userService.findByUserId(order.getSellMan());
        order.setSellName(user.getUsername());
        model.addAttribute("order", order);

        String status = cdService.getDescrByTypeAndCd(CdService.CD_ORDER_STATUS, order.getStatus());
        order.setStatusDescr(status);

        String rcptCmp = cdService.getDescrByTypeAndCd(CdService.CD_RECEIPT_COMP, order.getReceiptCompany());
        order.setRcptCmpDescr(rcptCmp);

        String contactName = contactService.selectById(order.getContactId()).getName();
        order.setContactName(contactName);

        boolean canAddReceipt = receiptService.canAddReceipt(order.getId());
        model.addAttribute("canAddReceipt", canAddReceipt);
    }
}
